part of 'view.dart';

final _realTimeData = ValueNotifier(<SafeRealTimeMonitorPageEntity>[]);

/// 安全监测 - APP - 实时监测数据
Future<void> _getRealTimeAlarm() async {
  final resp = await HttpUtil()
      .generateGetRequest<List<SafeRealTimeMonitorPageEntity>>(
        '${Interface.mainBaseUrl}/admin-api/system/alarm/getRecentAlarmMonitoring',
      )
      .execute(isShowToast: false);
  if (resp.isSucceed && resp.data != null) {
    _realTimeData.value = resp.data!;
  }
}

String _getAlarmType(int type) {
  switch (type) {
    case 1:
      return '一级';
    case 2:
      return '二级';
    case 3:
      return '三级';
    case 4:
      return '四级';
    default:
      return '';
  }
}

String _getAlarmLevel(int level) {
  switch (level) {
    case 1:
      return '一级';
    case 2:
      return '二级';
    case 3:
      return '三级';
    case 4:
      return '四级';
    default:
      return '';
  }
}

const _colors = [
  Color(0xff38DCD3),
  Color(0xffFF9A62),
  Color(0xffF4C64F),
  Color(0xff55B5F0),
  Color(0xff628EFF),
  Color(0xff54D09B),
];

final _alarmData = ValueNotifier(<ChartData>[]);

/// 报警历史
Future<void> _getAlarmHistory() async {
  final interval = _alarmInterval.value.startToEnd;
  final queries = {
    'alarmStartTime': '${interval.$1},${interval.$2}',
    'statisticsType': _alarmInterval.value.index + 1,
  };
  final queryStr = queries.entries.map((e) => '${e.key}=${e.value}').join('&').replaceAll(':', '%3A').replaceAll(',', '%2C').replaceAll(' ', '%20');
  final resp = await Dio(BaseOptions(
    headers: {'Authorization': UserManager.getToken()},
  )).get(
    '${Interface.mainBaseUrl}/admin-api/system/alarm/alarmStatistics?$queryStr',
  );
  if (resp.statusCode == 200) {
    try {
      final data = resp.data as List?;
      if (data == null) return;
      final datas = <ChartData>[];
      for (int idx = 0; idx < data.length; idx++) {
        final item = data[idx];
        try {
          datas.add(ChartData(
            item['companyName'] as String,
            item['num'] as num,
            _colors[idx % _colors.length],
          ));
        } catch (_) {}
      }
      _alarmData.value = datas;
    } catch (_) {}
  }
}
